home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games: Greatest Hits 1996 / Amiga Games: Greatest Hits 1996.iso / archive / userbox / publicdomain / engclock_v7.0.lha / EngClock_v7.0 / EngClock7_Source / sound.c < prev    next >
C/C++ Source or Header  |  1996-03-01  |  6KB  |  270 lines

  1. // Datatypes sound player - replacement for englock
  2. // WAS: play8svx.c
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <dos/dos.h>
  8. #include <clib/datatypes_protos.h>
  9. #include <clib/dos_protos.h>
  10. #include <clib/exec_protos.h>
  11. #include <clib/alib_protos.h>
  12. #include <clib/intuition_protos.h>
  13. #include <datatypes/datatypesclass.h>
  14. #include <datatypes/soundclass.h>
  15. #include <intuition/classes.h>
  16. #include <pragmas/xpkmaster_pragmas.h>
  17. #include <libraries/xpk.h>
  18. #include <libraries/xpksub.h>
  19.  
  20. extern struct Library *AslBase;
  21. extern struct IntuitionBase *IntuitionBase;
  22. extern struct GfxBase *GfxBase;
  23. extern struct Library *UtilityBase;
  24. extern struct Library *GadToolsBase;
  25. extern struct Library *DiskfontBase;
  26. extern struct Library *IFFParseBase;
  27. extern struct Library *DataTypesBase;
  28. extern struct Library *TranslatorBase;
  29. extern struct Library *DOSBase; 
  30. extern struct IntuiText ok_text;
  31.  
  32. struct Library *XpkBase;
  33.  
  34. void msg(char *msg); // Traditionally defined in here!
  35. char *checkxpk(char *name);
  36.  
  37.  
  38. struct IntuiText msg_text={
  39.       1,0,JAM1,16,9,NULL,NULL,NULL };
  40.  
  41. int playsam(char *esvxname);
  42. char *analysis(char *fname);
  43.  
  44. int playsam(char *esvxname) {
  45.     // Returns:
  46.         // 1 = OK
  47.         
  48.     struct dtTrigger Play;
  49.     struct DataType *dt;
  50.     struct DataTypeHeader *head;
  51.     Object *sound = NULL;
  52.     BPTR lock;
  53.     struct MsgPort *finish;
  54.     long bit;
  55.  
  56.     // WOn't continue with a non-null filename
  57.  
  58.     if(!(esvxname)) 
  59.         return(0);
  60.  
  61.     // Check whether file is compressed or not
  62.  
  63.     esvxname=(UBYTE *)checkxpk(esvxname);
  64.  
  65.     // OPen the lib first
  66.  
  67.     if(!(DataTypesBase=OpenLibrary("datatypes.library",0))) {
  68.         msg("Sound: Unable to open datatypes library");
  69.         return(2);
  70.     }
  71.  
  72.     if(!(lock=Lock(esvxname,ACCESS_READ))) {
  73.         msg("Sound: Could not find file!");
  74.         CloseLibrary(DataTypesBase);
  75.         return(3);
  76.     }
  77.  
  78.     if(!(dt=ObtainDataTypeA(DTST_FILE, (APTR)lock, NULL))) {
  79.         msg("Sound: Could not access file!");
  80.         UnLock(lock);
  81.         CloseLibrary(DataTypesBase);
  82.         return(4);
  83.     }
  84.  
  85.     if(!(finish=CreateMsgPort())) {
  86.         msg("Sound: Unable to set up reply port!");
  87.         UnLock(lock);
  88.         CloseLibrary(DataTypesBase);
  89.         ReleaseDataType(dt); 
  90.         return(5);
  91.     }
  92.  
  93.     bit=1<<finish->mp_SigBit;
  94.  
  95.     head=dt->dtn_Header;
  96.     
  97.     
  98.     if(head->dth_GroupID!=GID_SOUND) {
  99.         msg("Sound: File is not a sound type supported by system");
  100.         UnLock(lock);
  101.         CloseLibrary(DataTypesBase);
  102.         ReleaseDataType(dt);
  103.         DeleteMsgPort(finish);
  104.         return(6);
  105.     }
  106.  
  107.     ReleaseDataType(dt);
  108.     UnLock(lock);
  109.  
  110.    
  111.     if( sound = NewDTObject(esvxname,
  112.             DTA_GroupID, GID_SOUND, 
  113.             SDTA_Volume, 64, 
  114.             SDTA_Cycles, 1, 
  115.             SDTA_SignalTask, FindTask(NULL), 
  116.             SDTA_SignalBit, bit, 
  117.             TAG_DONE)) {
  118.  
  119.         Play.MethodID = DTM_TRIGGER;
  120.         Play.dtt_GInfo = NULL;
  121.         Play.dtt_Function = STM_PLAY;
  122.         Play.dtt_Data = NULL;
  123.         
  124.         DoDTMethodA(sound, NULL, NULL, (Msg)&Play);
  125.         
  126.         Wait(bit);
  127.         
  128.         DeleteMsgPort(finish);
  129.         DisposeDTObject(sound);
  130.         CloseLibrary(DataTypesBase);
  131.     }
  132.  
  133. }
  134.  
  135. char *analysis(char *fname) {
  136.     char text[500];
  137.     struct DataType *dt;
  138.     struct DataTypeHeader *head;
  139.     BPTR lock;
  140.     char type[100];
  141.     char newname[200];
  142.  
  143.     strcpy(newname,(UBYTE *)checkxpk(fname));
  144.  
  145.     // OPen the lib first
  146.  
  147.     if(!(DataTypesBase=OpenLibrary("datatypes.library",0))) {
  148.         msg("Sound: Unable to open datatypes library");
  149.         return(NULL);
  150.     }
  151.  
  152.     if(!(lock=Lock(&newname[0],ACCESS_READ))) {
  153.         msg("Sound: Could not find file!");
  154.         CloseLibrary(DataTypesBase);
  155.         return(NULL);
  156.     }
  157.  
  158.     if(!(dt=ObtainDataTypeA(DTST_FILE, (APTR)lock, NULL))) {
  159.         msg("Sound: Could not access file!");
  160.         UnLock(lock);
  161.         CloseLibrary(DataTypesBase);
  162.         return(NULL);
  163.     }
  164.  
  165.     head=dt->dtn_Header;
  166.  
  167.     
  168.     // Quick anaylsis
  169.  
  170.     // File can be:
  171.         // GID_PICTURE
  172.         // GID_SOUND
  173.         // GID_SYSTEM - dir, exe, font, device, lib, etc.
  174.         // GID_TEXT 
  175.         // GID_DOCUMENT
  176.         // GID_MUSIC
  177.         // GID_INSTRUMENT
  178.         // GID_ANIMATION
  179.         // GID_MOVIE
  180.     
  181.     
  182.  
  183.     if(head->dth_GroupID==GID_PICTURE) strcpy(type,"Picture"); else
  184.     if(head->dth_GroupID==GID_SOUND) {
  185.         sprintf(text,"Accepted! %s %s",head->dth_Name,"Sound");
  186.         msg(text);
  187.         ReleaseDataType(dt);
  188.         UnLock(lock);
  189.  
  190.         return(fname);
  191.     }
  192.     else
  193.  
  194.     if(head->dth_GroupID==GID_SYSTEM) strcpy(type,"System File"); else
  195.     if(head->dth_GroupID==GID_TEXT) strcpy(type,"Text File"); else
  196.     if(head->dth_GroupID==GID_DOCUMENT) strcpy(type,"Document"); else
  197.     if(head->dth_GroupID==GID_MUSIC) strcpy(type,"Music file"); else
  198.     if(head->dth_GroupID==GID_INSTRUMENT) strcpy(type,"Instrument"); else
  199.     if(head->dth_GroupID==GID_ANIMATION) strcpy(type,"Animation"); else
  200.     if(head->dth_GroupID==GID_MOVIE) strcpy(type,"Movie");
  201.  
  202.     sprintf(text,"Sorry file is in wrong format! (%s %s)",head->dth_Name, type);
  203.     msg(text);
  204.  
  205.     ReleaseDataType(dt);
  206.     UnLock(lock);
  207.     CloseLibrary(DataTypesBase);
  208.  
  209.     return(NULL);
  210. }
  211.  
  212. void msg(char *msg) {
  213.     msg_text.IText=msg;
  214.     ok_text.IText="Continue";
  215.     AutoRequest(NULL, &msg_text, NULL, &ok_text, (ULONG)NULL, (ULONG)NULL, 320, 100 );
  216. }
  217. char *checkxpk(char *name) {
  218.     struct XpkFib xfib;
  219.     BPTR file;
  220.     struct TagItem tags1[]={
  221.         XPK_InName,NULL,
  222.         TAG_DONE
  223.     };
  224.     struct TagItem tags2[]={
  225.         XPK_InName,NULL,
  226.         XPK_OutName,NULL,
  227.         TAG_DONE
  228.     };
  229.     
  230.     tags1[0].ti_Data=(ULONG)name;
  231.                 
  232.     if(!(XpkBase=OpenLibrary("xpkmaster.library",0))) {
  233.         /* Cannot open xpk! */
  234.         return(name);
  235.     }
  236.  
  237.     /* Is it packed ? */
  238.     XpkExamine(&xfib,tags1);
  239.  
  240.     if(xfib.Type!=XPKTYPE_PACKED) {
  241.         /* Not packed */
  242.         CloseLibrary(XpkBase);
  243.         return(name);
  244.     } else {
  245.         /* Packed so unpack it first */
  246.         /* set up tags */
  247.         tags2[0].ti_Data=(ULONG)name;
  248.         tags2[1].ti_Data=(ULONG)"ram:engclock.temp";    
  249.  
  250.         XpkUnpack(tags2);
  251.         /* Check if we wrote anything... */
  252.         file=Open("ram:engclock.temp",MODE_OLDFILE);
  253.         if(file) {
  254.             if(FGetC(file)==EOF) {
  255.                 msg("Error unpacking file! 1");
  256.                 CloseLibrary(XpkBase);
  257.                 Close(file);
  258.                 return(0L);
  259.             }
  260.         } else {
  261.             msg("Error unpacking file! 2");
  262.             CloseLibrary(XpkBase);
  263.             return(0L);
  264.         }
  265.         CloseLibrary(XpkBase);
  266.         Close(file);
  267.         return("ram:engclock.temp");
  268.  
  269.     }
  270. }